package com.application.gas;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.*;
import com.application.Formula;
import com.application.R;
import util.exceptions.InvalidDataException;
import util.tools.etc.Misc;
import util.tools.gas.GasData;
import util.tools.gas.GasLaws;

import java.util.ArrayList;

public class CombinedGas extends Activity {
    public String P1_units = "kPa", V1_units = "L", V2_units = "L", P2_units = "kPa", T1_units = "C", T2_units = "C";
    private int current;
    private static long last = 0;

    public void trySolve() {
        if (System.currentTimeMillis() - last < 10) {
            last = System.currentTimeMillis();
            return;
        }
        last = System.currentTimeMillis();
        GasData use, use2;

        double[] values = new double[6];
        values[0] = Misc.getValue(getEditText(R.id.combined_gas_P1_text).replaceAll(",", ""));
        values[1] = Misc.getValue(getEditText(R.id.combined_gas_V1_text).replaceAll(",", ""));
        values[2] = Misc.getValue(getEditText(R.id.combined_gas_T1_text).replaceAll(",", ""));
        values[3] = Misc.getValue(getEditText(R.id.combined_gas_P2_text).replaceAll(",", ""));
        values[4] = Misc.getValue(getEditText(R.id.combined_gas_V2_text).replaceAll(",", ""));
        values[5] = Misc.getValue(getEditText(R.id.combined_gas_T2_text).replaceAll(",", ""));

        try {

            if (values[0] != -1)
                values[0] = GasLaws.convertPressure(values[0], P1_units);
            if (values[1] != -1)
                values[1] = GasLaws.convertVolume(values[1], V1_units);
            if (values[2] != -1)
                values[2] = GasLaws.convertTemperature(values[2], T1_units);
            if (values[3] != -1)
                values[3] = GasLaws.convertPressure(values[3], P2_units);
            if (values[4] != -1)
                values[4] = GasLaws.convertVolume(values[4], V2_units);
            if (values[5] != -1)
                values[5] = GasLaws.convertTemperature(values[5], T1_units);

            if (current == R.id.combined_gas_P1_text)
                values[0] = -1;
            else if (current == R.id.combined_gas_V1_text)
                values[1] = -1;
            else if (current == R.id.combined_gas_T1_text)
                values[2] = -1;
            else if (current == R.id.combined_gas_P2_text)
                values[3] = -1;
            else if (current == R.id.combined_gas_V2_text)
                values[4] = -1;
            else if (current == R.id.combined_gas_T2_text)
                values[5] = -1;

            use = new GasData(values[0], values[1], values[2], 1);
            use2 = new GasData(values[3], values[4], values[5], 1);
            int id = -1;
            if (values[0] == -1 || current == R.id.combined_gas_P1_text)
                id = R.id.combined_gas_P1_text;
            else if (values[1] == -1 || current == R.id.combined_gas_V1_text)
                id = R.id.combined_gas_V1_text;
            else if (values[2] == -1 || current == R.id.combined_gas_T1_text)
                id = R.id.combined_gas_T1_text;
            else if (values[3] == -1 || current == R.id.combined_gas_P2_text)
                id = R.id.combined_gas_P2_text;
            else if (values[4] == -1 || current == R.id.combined_gas_V2_text)
                id = R.id.combined_gas_V2_text;
            else if (values[5] == -1 || current == R.id.combined_gas_T2_text)
                id = R.id.combined_gas_T2_text;
            if (id != -1) {
                double value = GasLaws.determine(use, use2);
                switch (current) {
                    case R.id.combined_gas_P1_text:
                        value = GasLaws.convertBackPressure(value, P1_units);
                        break;
                    case R.id.combined_gas_V1_text:
                        value = GasLaws.convertBackVolume(value, V1_units);
                        break;
                    case R.id.combined_gas_P2_text:
                        value = GasLaws.convertBackPressure(value, P2_units);
                        break;
                    case R.id.combined_gas_V2_text:
                        value = GasLaws.convertBackVolume(value, V2_units);
                        break;
                    case R.id.combined_gas_T2_text:
                        value = GasLaws.convertBackVolume(value, T2_units);
                    case R.id.combined_gas_T1_text:
                        value = GasLaws.convertBackVolume(value, T1_units);

                }
                if (value != Double.NEGATIVE_INFINITY) {
                    String result = Misc.round(value);
                    changeText(id, result);
                }
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (ArithmeticException e) {
            e.printStackTrace();
        } catch (InvalidDataException e) {
            changeText(current, "");
        }
    }

    private String getEditText(int resourceId) {
        return ((EditText) findViewById(resourceId)).getText().toString();
    }

    private void changeText(int resourceId, String s) {
        if (!(findViewById(resourceId) instanceof EditText))
            return;
        EditText ed = ((EditText) findViewById(resourceId));
        if (ed.getText().toString().equals(s))
            return;
        ((EditText) findViewById(resourceId)).setText(s);
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gas_layout_combined);
        current = R.id.combined_gas_P1_text;

        View[] radio = new View[6];
        ArrayList<View> ids = findViewById(R.id.combined_gas_radio_group_solve).getTouchables();
        ids.addAll(findViewById(R.id.combined_gas_radio_group_solve2).getTouchables());
        View[] text = new View[6];
        text[0] = findViewById(R.id.combined_gas_P1_text);
        text[1] = findViewById(R.id.combined_gas_V1_text);
        text[2] = findViewById(R.id.combined_gas_T1_text);
        text[3] = findViewById(R.id.combined_gas_P2_text);
        text[4] = findViewById(R.id.combined_gas_V2_text);
        text[5] = findViewById(R.id.combined_gas_T2_text);

        Spinner[] gen = new Spinner[6];
        gen[0] = (Spinner) findViewById(R.id.combined_gas_P1_spinner);
        gen[1] = (Spinner) findViewById(R.id.combined_gas_V1_spinner);
        gen[2] = (Spinner) findViewById(R.id.combined_gas_T1_spinner);
        gen[3] = (Spinner) findViewById(R.id.combined_gas_P2_spinner);
        gen[4] = (Spinner) findViewById(R.id.combined_gas_V2_spinner);
        gen[5] = (Spinner) findViewById(R.id.combined_gas_T2_spinner);
        char[] type = {'p', 'v', 't', 'p', 'v', 't'};
        Formula f = new Formula(ids.toArray(radio), text, gen, type, this) {
            @Override
            public void act(int i, Spinner c) {
                if (i == 0) {
                    P1_units = c.getSelectedItem().toString();
                } else if (i == 1) {
                    V1_units = c.getSelectedItem().toString();
                } else if (i == 2) {
                    T1_units = c.getSelectedItem().toString();
                } else if (i == 3) {
                    P2_units = c.getSelectedItem().toString();
                } else if (i == 4) {
                    V2_units = c.getSelectedItem().toString();
                } else if (i == 5) {
                    T2_units = c.getSelectedItem().toString();
                }
                current = getCurrent();
                trySolve();
            }

            @Override
            public void act(int cur) {
                current = cur;
                trySolve();
            }
        };
        RadioGroup.OnCheckedChangeListener checkListener = f.getCheckedChangeListener();
        ((RadioGroup) findViewById(R.id.combined_gas_radio_group_solve)).setOnCheckedChangeListener(checkListener);
        ((RadioGroup) findViewById(R.id.combined_gas_radio_group_solve2)).setOnCheckedChangeListener(checkListener);
        ((TextView) findViewById(R.id.combined_gas_P2_label)).setLineSpacing(1.5f, 1.5f);
        ((TextView) findViewById(R.id.combined_gas_V2_label)).setLineSpacing(1.5f, 1.5f);
        ((TextView) findViewById(R.id.combined_gas_P1_label)).setLineSpacing(1.5f, 1.5f);
        ((TextView) findViewById(R.id.combined_gas_T1_label)).setLineSpacing(1.5f, 1.5f);
        ((TextView) findViewById(R.id.combined_gas_T2_label)).setLineSpacing(1.5f, 1.5f);
        ((TextView) findViewById(R.id.combined_gas_V1_label)).setLineSpacing(1.5f, 1.5f);


        ((TextView) findViewById(R.id.combined_gas_P1_label)).setText(Html.fromHtml("<html>P<sub><small>1</small></sub></html>"));
        ((TextView) findViewById(R.id.combined_gas_V1_label)).setText(Html.fromHtml("<html>V<sub><small>1</small></sub></html>"));
        ((TextView) findViewById(R.id.combined_gas_P2_label)).setText(Html.fromHtml("<html>P<sub><small>2</small></sub></html>"));
        ((TextView) findViewById(R.id.combined_gas_V2_label)).setText(Html.fromHtml("<html>V<sub><small>2</small></sub></html>"));
        ((TextView) findViewById(R.id.combined_gas_T1_label)).setText(Html.fromHtml("<html>T<sub><small>1</small></sub></html>"));
        ((TextView) findViewById(R.id.combined_gas_T2_label)).setText(Html.fromHtml("<html>T<sub><small>2</small></sub></html>"));

        ((RadioButton) findViewById(R.id.combined_gas_radio_P1)).setText(Html.fromHtml("<html>P<sub><small>1</small></sub></html>"));
        ((RadioButton) findViewById(R.id.combined_gas_radio_V1)).setText(Html.fromHtml("<html>V<sub><small>1</small></sub></html>"));
        ((RadioButton) findViewById(R.id.combined_gas_radio_P2)).setText(Html.fromHtml("<html>P<sub><small>2</small></sub></html>"));
        ((RadioButton) findViewById(R.id.combined_gas_radio_V2)).setText(Html.fromHtml("<html>V<sub><small>2</small></sub></html>"));
        ((RadioButton) findViewById(R.id.combined_gas_radio_T1)).setText(Html.fromHtml("<html>V<sub><small>2</small></sub></html>"));
        ((RadioButton) findViewById(R.id.combined_gas_radio_T2)).setText(Html.fromHtml("<html>V<sub><small>2</small></sub></html>"));
    }
}